package bs.controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import bs.anno.Logger;
import bs.entity.PressBook;
import bs.entity.User;
import bs.service.BookService;
import bs.util.EasyUiUtil;
import bs.util.ResponseUtil;
import bs.util.ResultMessage;

/**
 * 
 * BookController:
 *
 * @author 姜治昊
 * @time 2017年11月3日 下午11:56:47
 */
@Controller
@RequestMapping("book")
public class BookController {

	@Autowired
	private BookService bookService;
	
	/**
	 * 获取热门图书
	 */
	@RequestMapping(value="hotbooks", method=RequestMethod.GET)
	public void getHotBooks() {
		
	}
	
	/**
	 * 获取我的图书
	 * @param response
	 * @param session
	 */
	@Logger(description="获取我的图书")
	@RequestMapping(value="mybooks", method=RequestMethod.GET)
	public void getMyBooks(
			 @RequestParam(required=false, defaultValue="1") int page
			,@RequestParam(required=false, defaultValue="10") int rows
			, HttpServletResponse response, HttpSession session) {
		User user = (User) session.getAttribute("LOGIN_USER");
		ResponseUtil.write(response, JSONArray.fromObject(bookService.getMyBooks(user.getUserId(), page, rows)));
	}
	
	/**
	 * 获取他人的图书
	 * @param id 用户ID
	 */
	@Logger(description="获取他人图书")
	@RequestMapping(value="friendbooks/{id}", method=RequestMethod.GET)
	public void getBooksByUser(
			 @RequestParam(required=false, defaultValue="1") int page
			,@RequestParam(required=false, defaultValue="10") int rows
			,@PathVariable("id") Integer id, HttpServletResponse response) {
		ResponseUtil.write(response, EasyUiUtil.dataGridFormat(rows, bookService.getMyBooks(id, page, rows)));
	}
	
	/**
	 * 根据作者获取图书
	 * @param id
	 * @param response
	 */
	@Logger(description="搜索作者图书")
	@RequestMapping(value="authorbooks/{name}")
	public void getBooksByAuthor(
			 @RequestParam(required=false, defaultValue="1") int page
			,@RequestParam(required=false, defaultValue="10") int rows
			,@PathVariable("name") String name, HttpServletResponse response) {
		ResponseUtil.write(response, JSONArray.fromObject(bookService.getBooksByAuthor(name, page, rows)));
	}
	
	/**
	 * 输入图书名获取图书
	 * @param name
	 * @param response
	 */
	@Logger(description="根据名称搜索图书")
	@RequestMapping(value="books/{name}")
	public void getBooksByName(
			 @RequestParam(required=false, defaultValue="1") int page
			,@RequestParam(required=false, defaultValue="10") int rows
			,@PathVariable("name") String name, HttpServletResponse response) {
		ResponseUtil.write(response, JSONArray.fromObject(bookService.getBooksByName(name, page, rows)));
	}
	
	/**
	 * 根据ISBN搜索图书
	 * @param response
	 * @param bookIsbn
	 */
	@Logger(description="根据ISBN搜索图书")
	@RequestMapping(value="isbn/{isbn}", method=RequestMethod.GET, produces="application/json;charset=UTF-8")
	public void getBookByIsbn(HttpServletResponse response, @PathVariable("isbn") String bookIsbn) {
		PressBook pBook = bookService.getBookByIsbn(bookIsbn);
		if(null != pBook) {
			ResponseUtil.write(response, JSONObject.fromObject(ResultMessage.createSuccessResultMessage(pBook)));
		} else {
			ResponseUtil.write(response, JSONObject.fromObject(ResultMessage.createErrorResultMessage("无记录")));
		}
	}
	
}
